package br.usp.ime.origami.main;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;

import br.usp.ime.origami.algorithms.Triangulation;
import br.usp.ime.origami.model.ColapsingTimeComparator;
import br.usp.ime.origami.model.MovingPoint;
import br.usp.ime.origami.model.MovingTriangle;
import br.usp.ime.origami.model.MovingTriangulation;
import br.usp.ime.origami.model.SimplePoint;
import br.usp.ime.origami.model.SimplePolygon;
import br.usp.ime.origami.util.PolygonReader;
import br.usp.ime.origami.view.Drawer;

public class Test implements KeyListener, MouseListener
{
	public static void main(String[] args) throws FileNotFoundException
	{
		new Test().run();
	}

	private MovingTriangulation movingTriangulation;

	private Drawer drawer;

	private int time;

	private void run() throws FileNotFoundException
	{
		JFrame frame = new JFrame();
		this.drawer = new Drawer();
		frame.addMouseListener(this);
		drawer.addMouseListener(this);

		frame.add(drawer);
		frame.setSize(800, 600);
		frame.setVisible(true);

		MovingPoint p1 = new MovingPoint(new SimplePoint(100, 100), Math.PI / 4, 1);
		MovingPoint p2 = new MovingPoint(new SimplePoint(100, 200), 7 * Math.PI / 4, 1);
		MovingPoint p3 = new MovingPoint(new SimplePoint(500, 100), 7 * Math.PI / 4, -1);
		MovingPoint p4 = new MovingPoint(new SimplePoint(500, 200), Math.PI / 4, -1);

		MovingTriangle t1 = new MovingTriangle(p2, p3, p4);
		MovingTriangle t2 = new MovingTriangle(p1, p2, p3);

		this.movingTriangulation = new MovingTriangulation();

		movingTriangulation.add(t1);
		movingTriangulation.add(t2);

		SimplePolygon p = PolygonReader.getPolygon(new FileInputStream("polygons\\aichholzer2.polygon"));
		List<MovingTriangle> triangles = new Triangulation(p).getTriangles();
		this.movingTriangulation = new MovingTriangulation(triangles);
		draw(0);

	}

	public void keyTyped(KeyEvent e)
	{
	}

	public void keyPressed(KeyEvent e)
	{
		// TODO Auto-generated method stub

	}

	public void keyReleased(KeyEvent e)
	{
		// TODO Auto-generated method stub

	}

	public void mouseClicked(MouseEvent e)
	{
		if (e.getButton() == MouseEvent.BUTTON1)
		{
			this.draw(2);
		}
		else
		{
			this.draw(-2);
		}
	}

	private void draw(double increment)
	{

		drawer.clear();

		List<MovingTriangle> list = movingTriangulation.getNotCollapsedMovingTrianglesAt(time);
		Collections.sort(list, new ColapsingTimeComparator());

		for (int i = 0; i < list.size(); i++)
		{
			if (i == 0)
				drawer.addFilledTriangle(list.get(i).getPositionAt(time), Color.RED);
			else if (i == 1)
				drawer.addFilledTriangle(list.get(i).getPositionAt(time), Color.YELLOW);
			else
				drawer.addTriangle(list.get(i).getPositionAt(time), Color.GREEN);

		}

		for (MovingTriangle t : movingTriangulation.getMovingTriangles())
		{

			// drawer.addTriangle(t.getPositionAt(0), Color.RED);
			// drawer.addTriangle(t.getPositionAt(time), Color.BLUE);
		}

		drawer.repaint();

		this.time += increment;
	}

	public void mousePressed(MouseEvent e)
	{

	}

	public void mouseReleased(MouseEvent e)
	{

	}

	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

}
